//
// Created by user on 2022/3/22.
//
#include <algorithm>
using namespace std;
struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode() : val(0), left(nullptr), right(nullptr) {}
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};
int res = 0;
int maxDepth(TreeNode* root) {
    if(!root)return 0;
    int left = maxDepth(root->left);
    int right = maxDepth(root->right);
    res = max(res, left+right);

    return max(left,right)+1;
}
int diameterOfBinaryTree(TreeNode* root) {
    if (!root) return res;
    maxDepth(root);

    return res;

}
